package com.huilian.iotbox.admin.wap.controller;

import com.huilian.iotbox.admin.wap.service.QualificationService;
import com.huilian.iotbox.admin.wap.service.UserService;
import com.huilian.iotbox.data.common.ServerResponse;
import com.huilian.iotbox.data.controller.BaseController;
import com.huilian.iotbox.data.dto.QualificationDto;
import com.huilian.iotbox.data.enums.QualificationTypeEnum;
import com.huilian.iotbox.data.po.Qualification;
import com.huilian.iotbox.data.vo.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author 谢洛涛
 * @date 2021/6/2 12:03
 */
@RestController
    @RequestMapping("/api/qualification")
public class QualificationController extends BaseController {
    @Autowired
    private QualificationService qualificationService;
    @Autowired
    private UserService userService;
    @GetMapping("/findQualificationListByUserId")
    public ServerResponse findQualificationListByUserId(HttpServletRequest request,Qualification qualification){
        UserInfo userInfo = userService.getLoginUserInfo(request);
        qualification.setUserId(userInfo.getId());
        return ServerResponse.createBySuccess(qualificationService.findQualificationList(qualification));
    }

    /**
     * 查询分账商户数量，count
     * */
    @GetMapping("/findSubLedgerMerchantCountByUserId")
    public ServerResponse findSubLedgerMerchantCountByUserId(Qualification qualification, HttpServletRequest request){
        UserInfo userInfo = userService.getLoginUserInfo(request);
        qualification.setUserId(userInfo.getId());
        return qualificationService.findSubLedgerMerchantCountByUserId(qualification);
    }


    @PostMapping("/findQualificationInfo")
    public ServerResponse findQualificationInfo(HttpServletRequest request, @RequestBody Qualification qualification) throws Exception {
        UserInfo userInfo = userService.getLoginUserInfo(request);
        if(ObjectUtils.isEmpty(userInfo)){
            return ServerResponse.createByErrorMessage("获取商户信息失败");
        }
        return qualificationService.findQualificationInfo(qualification);
    }

    @PostMapping("qualificationRegister")
    public ServerResponse qualificationRegister(HttpServletRequest request, @RequestBody QualificationDto qualificationDto) throws Exception {
        UserInfo userInfo = userService.getLoginUserInfo(request);
        if(ObjectUtils.isEmpty(userInfo)){
            return ServerResponse.createByErrorMessage("获取商户信息失败");
        }
        return qualificationService.qualificationRegister(qualificationDto);
    }

    @PostMapping("/findMerchantIdByQualificationId")
    public ServerResponse findMerchantIdByQualificationId(HttpServletRequest request, @RequestBody Qualification qualification) throws Exception {
        UserInfo userInfo = userService.getLoginUserInfo(request);
        if(ObjectUtils.isEmpty(userInfo)){
            return ServerResponse.createByErrorMessage("获取商户信息失败");
        }
        qualification.setUserId(userInfo.getId());
        return qualificationService.findMerchantIdByQualificationId(qualification);
    }

    /**
     * 根据userId 和 Type 查询商户的信息
     * */
    @PostMapping("/findQualificationByType")
    public ServerResponse findQualificationByType(HttpServletRequest request, @RequestBody Qualification qualification) throws Exception {
        UserInfo userInfo = userService.getLoginUserInfo(request);
        qualification.setUserId(userInfo.getId());
        return qualificationService.findQualificationByType(qualification);
    }
}
